package org.brisling.workflow.service;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class WorkFlowOperatorBackup {
	
	private static Logger logger = LoggerFactory.getLogger(WorkFlowOperatorBackup.class);
	@Autowired
    private RuntimeService runtimeService;

    @SuppressWarnings("unused")
	@Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RepositoryService repositoryService;
	/** 
     * 根据实例编号查找下一个任务节点 
     * @param String procInstId ：实例编号 
     * @return 
     */  
    public TaskDefinition nextTaskDefinition(String procInstId){  
        //流程标示  
        String processDefinitionId = historyService.createHistoricProcessInstanceQuery().processInstanceId(procInstId).singleResult().getProcessDefinitionId();  
          
        ProcessDefinitionEntity def = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processDefinitionId);  
        //执行实例  
        ExecutionEntity execution = (ExecutionEntity) runtimeService.createProcessInstanceQuery().processInstanceId(procInstId).singleResult();  
        //当前实例的执行到哪个节点  
        String activitiId = execution.getActivityId();  
        //获得当前任务的所有节点  
        List<ActivityImpl> activitiList = def.getActivities();  
        String id = null;  
        for(ActivityImpl activityImpl:activitiList){    
            id = activityImpl.getId();   
            if(activitiId.equals(id)){  
                System.out.println("当前任务："+activityImpl.getProperty("name"));  
                return nextTaskDefinition(activityImpl, activityImpl.getId(),"${iscorrect==1}");  
//              System.out.println(taskDefinition.getCandidateGroupIdExpressions().toArray()[0]);  
//              return taskDefinition;  
            }  
        }  
        return null;  
    }  
    
    /** 
     * 下一个任务节点 
     * @param activityImpl 
     * @param activityId 
     * @param elString 
     * @return 
     */  
    public TaskDefinition nextTaskDefinition(ActivityImpl activityImpl, String activityId, String elString){  
            if("userTask".equals(activityImpl.getProperty("type")) && !activityId.equals(activityImpl.getId())){  
                TaskDefinition taskDefinition = ((UserTaskActivityBehavior)activityImpl.getActivityBehavior()).getTaskDefinition();  
//              taskDefinition.getCandidateGroupIdExpressions().toArray();  
                return taskDefinition;  
            }else{  
                List<PvmTransition> outTransitions = activityImpl.getOutgoingTransitions();  
                List<PvmTransition> outTransitionsTemp = null;  
                for(PvmTransition tr:outTransitions){    
                    PvmActivity ac = tr.getDestination(); //获取线路的终点节点    
                    if("exclusiveGateway".equals(ac.getProperty("type"))){  
                        outTransitionsTemp = ac.getOutgoingTransitions();  
                        if(outTransitionsTemp.size() == 1){  
                            return nextTaskDefinition((ActivityImpl)outTransitionsTemp.get(0).getDestination(), activityId, elString);  
                        }else if(outTransitionsTemp.size() > 1){  
                            for(PvmTransition tr1 : outTransitionsTemp){  
                                Object s = tr1.getProperty("conditionText"); 
                                if((s!=null)&&elString.equals(s.toString())){  
                                    return nextTaskDefinition((ActivityImpl)tr1.getDestination(), activityId, elString);  
                                }  
                            }  
                        }  
                    }else if("userTask".equals(ac.getProperty("type"))){  
                        return ((UserTaskActivityBehavior)((ActivityImpl)ac).getActivityBehavior()).getTaskDefinition();  
                    }else{  
                        logger.debug("type: {} ",ac.getProperty("type"));  
                    }  
                }   
            return null;  
        }  
    }  

}
